<?
	/**
	 * UCModActive.php
	 * 激活码验证逻辑
	 *
	 * Copyright (c) 2010 snda.com - Web Development Center
	 * @author: Gu Shougang <gushougang@snda.com>
	 */
	
	require_once(dirname(__FILE__).'/../../config/UCConfActive.php');
	require_once(dirname(__FILE__).'/../../libraries/UCFuncUtils.php');
	require_once(dirname(__FILE__).'/../../libraries/UCLibPDOFactory.php');
	
	class UCModActive
	{
		public static $_debug = FALSE;
		/**
		 * 构造函数
		 * 调用__construct()
		 */
		public function UCModActive()
		{
			$this->__construct();
		}
		
		/**
		 * 构造函数
		 */
		public function __construct()
		{
			// Do nothing
		}
		
		public static function debug($message)
		{
			if(self::_debug)
			{
				echo "[" . date('Y-m-d H:i:s') . "]" . $message . "\n";
			}
		}
		
		/**
		 * 检查激活码是否过期
		 */
		public static function checkActiveTime()
		{
			$now = time();
			if($now >= ACTIVE_CODE_START && $now <= ACTIVE_CODE_END)
			{
				return TRUE;
			}
			return FALSE;
		}
		
		/**
		 * 生成激活码
		 * 
		 * @param int $limit	生成激活码数量
		 * @return string/array/boolean	如果$limit =1,则返回一个字符串,如果$limit > 1,则返回数组
		 */
		public static function makeActiveCode($limit = 1)
		{
			if(self::checkActiveTime() === FALSE)
			{
				return FALSE;
			}
			
			if(!is_numeric($limit))
			{
				return FALSE;
			}
			$limit = trim($limit);
			
			if($limit < 1)
			{
				$limit = 1;
			}
			
			if($limit > MAX_ACTIVE_CODE_COUNT)
			{
				$limit = MAX_ACTIVE_CODE_COUNT;
			}
			
			if($limit == 1)
			{
				$active_code = make_rand_string(ACTIVE_CODE_CHARACTER);
				
				$r = self::setActiveCodeToDB($active_code);
				
				if($r[$active_code] === FALSE)
				{
					return FALSE;
				}
				
				return $active_code;
			}
			else
			{
				for($i = 0; $i < $limit; $i++)
				{
					$active_code[] = make_rand_string(ACTIVE_CODE_CHARACTER);
				}
				
				$r = self::setActiveCodeToDB($active_code);
				
				$return_array = array();
				foreach($active_code as $code)
				{
					if($r[$code] === TRUE)
					{
						$return_array[] = $code;
					}
				}
				
				return $return_array;
			}
		}
		
		/**
		 * 查询某个UID是否输入过激活码
		 *
		 * @param int $uid 用户ID
		 * @param int $
		 * @return string
		 */
		public static function checkActiveCodeByUID($uid)
		{
			$uid = trim($uid);
			if(strlen($uid) == 0 || !is_numeric($uid))
			{
				return FALSE;
			}
			
			$r = self::getActiveCodeToDB(array('active_uid' => $uid));
			
			return (count($r)) ? $r[0]['active_code'] : '';
		}
		
		/**
		 * 校验激活码是否被激活过
		 *
		 * @param string $active_code
		 * @return boolean	TRUE表示该激活码可用,FALSE表示该激活码不可用
		 */
		public static function checkActiveCode($active_code)
		{
			$active_code = trim($active_code);
			if(strlen($active_code) != ACTIVE_CODE_CHARACTER)
			{
				return FALSE;
			}
			
			$r = self::getActiveCodeToDB(array('active_code' => $active_code));
			
			if($r[0]['active_status'] == ACTIVE_CODE_UNACTIVED)
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
		}
		
		/**
		 * 将生成的激活码存入数据库
		 * 
		 * @param string $active_code
		 * @return array
		 */
		public static function setActiveCodeToDB($active_code)
		{
			if(self::checkActiveTime() === FALSE)
			{
				return FALSE;
			}
			
			if(is_string($active_code))
			{
				$active_code = trim($active_code);
				if(strlen($active_code) != ACTIVE_CODE_CHARACTER)
				{
					return FALSE;
				}
				$temp = $active_code;
				$active_code = array();
				$active_code[] = $temp;
			}
			else if(is_array($active_code))
			{
				//
			}
			else
			{
				return FALSE;
			}

			$pdo = UCLibPDOFactory::getPDO('', '', 'active2dbconfig');

			$return_array = array();
			foreach($active_code as $code)
			{
				$code = trim($code);
				if(strlen($code) != ACTIVE_CODE_CHARACTER)
				{
					$return_array[$code] = FALSE;
				}
				else
				{
					$sql = 'INSERT INTO ' . $pdo->database . '.' . $pdo->table . ' SET `active_code`=?, `active_status`=?, `gen_timestamp`=?';
					$h = $pdo->exec_with_prepare($sql, array($code, ACTIVE_CODE_UNACTIVED, date('Y-m-d H:i:s')));
					$return_array[$code] = ($h) ? TRUE : FALSE;
				}
			}
			
			return $return_array;
		}
		
		/**
		 * 从数据库中获取指定邀请码的信息
		 * 
		 * @param string $active_code
		 * @return boolean
		 */
		public static function getActiveCodeToDB($params = array(), $need_count = FALSE, $start = '', $limit = 0)
		{
			if(self::checkActiveTime() === FALSE)
			{
				return FALSE;
			}
			
			$pdo = UCLibPDOFactory::getPDO('', '', 'active2dbconfig');
			
			$select = ($need_count) ? 'COUNT(1) AS NUM' : '*';
			$sql = 'SELECT ' . $select . ' FROM ' . $pdo->database . '.' . $pdo->table . ' WHERE 1=1';
			
			$query_string = '';
			$fields = array_keys($params);
			foreach($fields as $field)
			{
				$query_string .= ' and ' . $field . '=?';
			}
			
			if(strlen($query_string))
			{
				$sql .= $query_string;
			}
			
			if(!is_numeric($limit))
			{
				return FALSE;
			}
			
			if($limit > 0)
			{
				if(!is_numeric($start))
				{
					return FALSE;
				}
				if($start < 0)
				{
					return FALSE;
				}
				
				$sql .= ' LIMIT ' . $start . ', ' . $limit;
			}
			
			$values = array_values($params);
			
			if($need_count)
			{
				$h = $pdo->query_with_prepare($sql, $values);
				return $h[0]['NUM'];
			}
			return $pdo->query_with_prepare($sql, $values);
		}
		
		/**
		 * 使用邀请码
		 * 
		 * @param string $active_code
		 * @param int $active_status
		 * @param int $active_uid
		 * @return boolean
		 */
		public static function updateActiveCodeToDB($active_code, $active_status, $active_uid)
		{
			if(self::checkActiveTime() === FALSE)
			{
				return FALSE;
			}
			
			$active_code = trim($active_code);
			if(strlen($active_code) != ACTIVE_CODE_CHARACTER)
			{
				return FALSE;
			}
			
			if($active_status != ACTIVE_CODE_UNACTIVED && $active_status != ACTIVE_CODE_ACTIVED && $active_status != ACTIVE_CODE_BAD)
			{
				return FALSE;
			}
			
			$pdo = UCLibPDOFactory::getPDO('', '', 'active2dbconfig');
			
			$sql = 'UPDATE ' . $pdo->database . '.' . $pdo->table . ' SET `active_status`=?, `active_timestamp`=?, `active_uid`=? WHERE `active_code` = ?';
			$h = $pdo->exec_with_prepare($sql, array($active_status, date('Y-m-d H:i:s'), $active_uid, $active_code));
			
			return ($h) ? TRUE : FALSE;
		}
	}
?>